//
//  GoodEvaluateListViewController.m
//  B2C
//
//  Created by xiaowu on 2017/8/4.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodEvaluateListViewController.h"
#import "GoodCommentCell.h"
#import "GoodEvaluateModel.h"
#import "PageNationModel.h"

#define TestImageArr  @[@"http://pic.58pic.com/58pic/16/38/45/03i58PICDKP_1024.jpg",@"http://pic5.nipic.com/20091222/2905177_131009095824_2.jpg",@"http://img.sucai.redocn.com/attachments/images/201201/20120109/20111229_b786e39d3c820e367af8G5BwN8lSdvxX.jpg",@"http://pic.58pic.com/58pic/16/38/47/29Y58PICtyW_1024.jpg"]


@interface GoodEvaluateListViewController ()<UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) PageNationModel *pageNationModel;
@end

@implementation GoodEvaluateListViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        _dataArr = [NSMutableArray array];
        _pageNationModel = [[PageNationModel alloc] init];
        _pageNationModel.pageNumber = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configureUI];
    [self getEvaluateList];
}

- (void)configureUI {
    self.title = @"全部评价";
    self.view.backgroundColor = [UIColor backgroundColor];
    self.tableView.backgroundColor = [UIColor backgroundColor];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView clearExtendCellLine];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodCommentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"commentCell"];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNationModel.pageNumber = 1;
        [weakSelf getEvaluateList];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageNationModel.pageNumber += 1;
        [weakSelf getEvaluateList];
    }];
}

#pragma mark - Network
- (void)getEvaluateList {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safeSetObject:self.gid forKey:@"gid"];
    [parameters safeSetObject:@(self.pageNationModel.pageNumber) forKey:@"pageNumber"];
    [parameters safeSetObject:PageSize forKey:@"pageSize"];
    
    __weak typeof(self) weakSelf = self;
    //    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Get_Evaluate parameters:parameters success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        NSDictionary *responseDic = responseObject;
        NSString *code = [responseDic safeStringForKey:@"code"];
        if (code && [code isEqualToString:@"200"]) {
            NSArray *data = [responseDic safeJsonObjectForKey:@"data"];
            
            NSDictionary *nation = [responseDic safeJsonObjectForKey:@"pagenation"];
            weakSelf.pageNationModel = [PageNationModel mj_objectWithKeyValues:nation];
            
            if (weakSelf.pageNationModel.totalPage <= (weakSelf.pageNationModel.pageNumber * weakSelf.pageNationModel.pageSize)) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [weakSelf.tableView.mj_footer resetNoMoreData];
            }
            
            if (weakSelf.pageNationModel.pageNumber == 1) {
                [weakSelf.dataArr removeAllObjects];
            }
            
            for (NSDictionary *modelDic  in data) {
                GoodEvaluateModel *model = [GoodEvaluateModel mj_objectWithKeyValues:modelDic];
                [weakSelf.dataArr addObject:model];
            }
            [weakSelf.tableView.mj_footer setHidden:(weakSelf.dataArr.count == 0)];
            [weakSelf.tableView reloadData];
            
        } else {
            [MBProgressHUD showError:weakSelf.view title:[responseDic safeStringForKey:@"message"] code:nil animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        
    } isNeedCache:NO];
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 10;
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    GoodCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"commentCell" forIndexPath:indexPath];
    GoodEvaluateModel *model = [self.dataArr safeObjectAtIndex:indexPath.row];
    [commentCell setValueForCellWithContent:model.econtent headUrl:model.mimg time:model.createtime nickNamek:model.mnickname point:model.escore withImages:[model getImgsUrl] isAnonymous:model.isanonymous];

//    [commentCell setValueForCellWithContent:@"这是一个测试内容" headUrl:@"" time:@"2018-08-01 09:59" nickNamek:@"这是个测试用户" point:@"5.0" withImages:TestImageArr];
    commentCell.picturesCell.block = ^(UIImageView *imageView, NSInteger index) {
        [weakSelf showPicture:imageView index:index urls:[model getImgsUrl]];
    };
    return commentCell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodEvaluateModel *model = [self.dataArr safeObjectAtIndex:indexPath.row];

    return [GoodCommentCell getHeightWithContent:model.econtent withImages:[model getImgsUrl]];
}

#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"暂无评价"
                                           attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0],
                                                        NSForegroundColorAttributeName:[UIColor grayTextColor]}];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    self.pageNationModel.pageNumber = 1;
    [self getEvaluateList];
}

@end
